package com.huluobo.h3kotlindemo.cart

import android.annotation.SuppressLint
import android.util.Log
import android.widget.CheckBox
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.h3kotlindemo.R
import com.huluobo.h3kotlindemo.base.BaseMVVMFragment
import com.huluobo.h3kotlindemo.databinding.FragmentCartBinding


class CartFragment : BaseMVVMFragment<FragmentCartBinding>() {

    private lateinit var viewModel: CartViewModel
    private lateinit var cartAdapter: CartAdapter

    private val cartList = mutableListOf<CartBean>()
    override fun getLayoutId(): Int = R.layout.fragment_cart

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[CartViewModel::class.java]

        viewModel.getCartList()

        viewModel.getCartListSuccess.observe(this) { it ->
            if (it.code == 200) {
                cartList.clear()
                cartList.addAll(it.data)
                cartAdapter.notifyDataSetChanged()
            }
        }

        viewModel.addCartSuccess.observe(this) {
            Log.i("TagA", "购物车数量+1")
        }

        viewModel.removeCartSuccess.observe(this) {
            Log.i("TagA", "购物车数量-1")
        }
    }

    override fun initView() {
        dataBinding.cartRv.layoutManager = LinearLayoutManager(requireContext())
        cartAdapter = CartAdapter(cartList)
        dataBinding.cartRv.adapter = cartAdapter

        dataBinding.cartCbSelectAll.setOnCheckedChangeListener { buttonView, isChecked ->
            if (cartList.size > 0) {
                cartList.forEach {
                    it.selected = isChecked
                }
            }
            cartAdapter.notifyDataSetChanged()
            totalPrice()
        }

        cartAdapter.addChildClickViewIds(
            R.id.cart_item_cb,
            R.id.cart_item_iv_reduce,
            R.id.cart_item_iv_add,
            R.id.cart_item_iv_delete
        )

        cartAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.cart_item_cb -> {
                    cartList[position].selected = !cartList[position].selected
                    (adapter.getViewByPosition(position, R.id.cart_item_cb) as CheckBox).isChecked =
                        cartList[position].selected
                }

                R.id.cart_item_iv_reduce -> {
                    if (cartList[position].count > 1) {
                        cartList[position].count -= 1
                        //还需要去请求购物车减少的接口
                        viewModel.removeCart(-1, cartList[position].goods_id)
                        (adapter.getViewByPosition(position, R.id.cart_item_tv_num) as TextView).text =
                            cartList[position].count.toString()
                    }
                }

                R.id.cart_item_iv_add -> {
                    cartList[position].count += 1
                    //还需要去请求购物车减少的接口
                    viewModel.addOneCart(1, cartList[position].goods_id)
                    (adapter.getViewByPosition(position, R.id.cart_item_tv_num) as TextView).text =
                        cartList[position].count.toString()
                }

                R.id.cart_item_iv_delete -> {
                    viewModel.deleteCart(cartList[position].id.toString())
                    cartList.removeAt(position)
                    adapter.notifyItemRemoved(position)
                }
            }
            totalPrice()
        }
    }

    private fun totalPrice() {
        var totalPrice = 0

        val checkSet = mutableSetOf<Boolean>()
        cartList.forEach {
            if (it.selected) {
                totalPrice += it.goods_default_price * it.count
            }
            checkSet.add(it.selected)
        }

        dataBinding.cartCbSelectAll.isChecked = false !in checkSet

        dataBinding.cartTvTotalPrice.text = "总价:${totalPrice}"

    }

}